<?php
namespace app\controller\model;

use app\BaseController;
use think\facade\Db;

class Users extends BaseController
{
    protected $tables;
    public function __construct(){
        $this->tables=new Tables();
    }
    public function calcBonus($userID=0,$orderID=0,$number=1){
        if($number>2){
            return false;
        }
        $order=false;
        $user=false;
        if(is_numeric($userID)){
            $user = r_one($this->tables->users,"id=$userID");
        }elseif (is_array($userID)){
            $user = $userID;
        }
        if(!$user){return false;}
        if(is_numeric($orderID)){
            $order = r_one($this->tables->orders,"id=$orderID");
        }elseif (is_array($orderID)){
            $order = $orderID;
        }
        if(!$order){return false;}
        $bonusClass = r_classes(219);
        $bonusNumber1 = $bonusClass[0]['rule'];
        $bonusNumber2 = $bonusClass[1]['rule'];
        lock();
        $bonusNumber=$bonusNumber1;
        if($number==1){
            if($user['pathid']<1){
                $bonusNumber=$bonusNumber2;
            }
        }
        if($bonusNumber<1){
            $amount =  bcmul($order['amount'],$bonusNumber,2);
        }else{
            $amount = $bonusNumber * $order['number'];
        }
        $this->userAddAmount($user['id'],$amount,3,$order['id'],'',substr_cut($user['nickname']).'刚才通过推广赚了');
        unlockCommit();
        if($number==1 && $user['pathid']>0 && $user['level']<2){
            $this->calcBonus($user['pathid'],$order,$number+1);
        }
    }
    public function userGoodsPrice($user=[],$goods=[]){
        //处理用户价格
        if(!isset($user['level']) || !isset($goods['price2'])){return false;}
        $level = $user['level'];
        if($level==1){
            $goods['price2']=148;
        }
        if($level==2){
            $goods['price2']=118;
        }
        return $goods;
    }
    /**
     * @param $userID 用户ID
     * @param $amount 金额
     * @param $payWay 支付方式，0微信，1余额，100系统
     * @param $orderID 订单号
     * @param $payNumbers 相关支付订单号 只有微信支付有
     * @return false|void
     * @throws \think\db\exception\DbException
     */
    public function userAddAmount($userID=0,$amount=0,$payWay=0,$orderID=0,$payNumbers='',$content=''){
        //用户增减金额
        lock();
        $user = r_one($this->tables->users,"id=$userID");
        if(!$user){
            unlockRollback();
            return false;
        }
        if($payWay!=0){
            $up = DB::name($this->tables->users)->where("id",$userID)->inc("amount",$amount)->update();
            $endAmount = bcadd($user['amount'],$amount,2);
        }else{
            $up=true;
            $endAmount = $user['amount'];
        }
        if($up){
            //写日志
            $orderTitle='';
            $price=0;
            $number=0;
            if($orderID>0){
                $order = r_one($this->tables->orders,"id=$orderID");
                if($order){
                    $orderTitle=$order['goodsTitle'];
                    $price=$order['price'];
                }
            }
            $logArr = ['uid'=>$userID,'payWay'=>$payWay,'startAmount'=>$user['amount'],'endAmount'=>$endAmount,'payNumbers'=>$payNumbers,'orderID'=>$orderID,'orderTitle'=>$orderTitle,'price'=>$price,'number'=>$number,'amount'=>$amount,'addTime'=>nowTime(),'content'=>$content];
            if($payWay==2){
                //提现的状态为0
                $logArr['status']=0;
            }
            $log = DB::name($this->tables->userAmountLog)->insert($logArr);
            if(!$log){
                unlockRollback();
                return false;
            }
            if($payWay==2){
                DB::name($this->tables->users)->where("id",$userID)->inc("amountING",0-$amount)->update();
            }
            unlockCommit();
            return true;
        }
    }
    public function amountLogStatusStr($res=[]){
        if(!is_array($res) || count($res)<1){
            return $res;
        }
        foreach ($res as $k=>$v){
            switch ($v['payWay']){
                case 0:
                    $res[$k]['payWay']="微信支付";
                    break;
                case 1:
                    $res[$k]['payWay']="余额支付";
                    break;
                case 2:
                    $res[$k]['payWay']="提现";
                    break;
                case 3:
                    $res[$k]['payWay']="推广";
                    break;
                case 100:
                    $res[$k]['payWay']="系统操作";
                    break;
                default:
                    $res[$k]['payWay']="未知";
            }
            switch ($v['status']){
                case 0:
                    $res[$k]['status']="待审核";
                    break;
                case 1:
                    $res[$k]['status']="完成";
                    break;
                default:
                    $res[$k]['payWay']="未知";
            }
        }
        return $res;
    }
    public function setLevel($user=[]){
        if(!$user || !is_array($user)){return false;}
        $userID = $user['id'];
        if($user['level']==1){
            $countUser = DB::name($this->tables->users)->where("pathid",$userID)->where("level",">",0)->count();
            if($countUser>1){
                DB::name($this->tables->users)->where("id",$userID)->update(['level'=>2,'levelStr'=>'合伙人']);
                //升级合伙人成功，拆分3个会员给上级
                if($user['pathid']>0){
                    $pathID=$user['pathid'];
                    $pathUser = r_one($this->tables->users,"id=$pathID");
                    if($pathUser && $pathUser['level']>=2){
                        DB::name($this->tables->users)->where("pathid",$userID)->where("level",">",0)->limit(2)->update(['pathid'=>$pathID,'pathIdOld'=>$userID]);
                        DB::name($this->tables->users)->where("id",$pathID)->inc("users",2)->update();
                    }
                }
                return true;
            }
        }
        if($user['level']==0){
            $countOrder = DB::name($this->tables->orders)->where("status",">",0)->where("afterSellStatus",0)->count();
            if($countOrder>0){

                DB::name($this->tables->users)->where("id",$userID)->update(['level'=>1,'levelStr'=>'会员']);
                //升级正式会员成功 产看上级是否需要升级
                if($user['pathid']>0){
                    $pathID=$user['pathid'];
                    //升级下单人数加1
                    DB::name($this->tables->users)->where("id",$pathID)->inc("usersNumber")->update();
                    $pathUser = r_one($this->tables->users,"id=$pathID");
                    if($pathUser && $pathUser['level']==1){
                        $this->setLevel($pathUser);
                    }
                }
                return true;
            }
        }
        return false;
    }
}
?>